/*
* Class: RssFeedFactory
* Description: Abstract Class RssFeedFactory is used to create create RssFeed objects
* and validate there creation. 
*
* Note:
* Date: March 28, 2012
* Author: Shane Swayney
*/

package rssreaderapp;

import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Abstract Class RssFeedFactory is used to create create RssFeed objects
 * and validate there creation. 
 * 
 * @author Shane swayney <shane.swayney@gmail.com>
 */
public abstract class RssFeedFactory {
    
    /**
     * Method to create an {@link RssFeed} from a string containing the url of a
     * RSS feed.
     * <p>
     * Note: Throws Exception so that {@link RssReaderFrame} can output a relevant
     * response when user enters a url to create a new RssFeed. The Exception
     * is generated by {@link RssFeedStreamParser}s open method.
     * 
     * @param urlFile a string containing a url to access the RSS feed
     * @return a new RssFeed containing all proper tags and fields.
     * @throws Exception to be used by caller for info
     */
    public static RssFeed createRssFeed(String urlFile) throws Exception
    {
        //Create the feed from a url.
        RssFeed rssFeed = new RssFeed(urlFile);
        
        //Create an XML parser to parse rss file
        RssFeedStreamParser parser = new RssFeedStreamParser(urlFile);
        //Open the stream: Throws exception if url is bad. 
        parser.openStream();
        //get the files last modified date
        String lastModified = getURLLastModifiedDate(urlFile);
        rssFeed.setLastModified(lastModified);
        

        //get the title of the rss Channel and link
        String titleElement = parser.getNextTextByTag(RssFeedStreamParser.RSSFEEDTAGS.TITLE);
        String urlElement = parser.getNextTextByTag(RssFeedStreamParser.RSSFEEDTAGS.LINK);
        
        //Set the Title of the RssFeed
        rssFeed.setRssTitle(titleElement);
        
            //Begin adding components of an rss feed to the RssFeed componentsArrayList
        
            //Add the channel in the rssComponentsArraylist as a titleitem
            rssFeed.addChannelComponent(titleElement, urlElement);

            //Add the rest as feed items
            do
            {
                titleElement = parser.getNextTextByTag(RssFeedStreamParser.RSSFEEDTAGS.TITLE);
                urlElement = parser.getNextTextByTag(RssFeedStreamParser.RSSFEEDTAGS.LINK);
                rssFeed.addItemComponent(titleElement, urlElement);

            }while(!titleElement.isEmpty() && !urlElement.isEmpty());

 
        return rssFeed;
    }
    

    /**
     * Static method to get the date of the last modification to the url file
     * generating the RSS feed. This method is in use by {@link RssReaderFrame}
     * to decide if the RSS feed needs to be updated. 
     * <p>
     * 
     * @see getURLLastModifiedDate
     * 
     * @param rssFeed an {@link RssFeed} Object
     * @return a string containing the last modified date of the url file
     */
    public static String getRssFeedLastModifiedDate(RssFeed rssFeed)
    {
        String lastModifiedDate = "";
        try {
                lastModifiedDate = new URL(rssFeed.getRssURL()).openConnection().getHeaderField("Last-Modified");
            } catch (IOException ex) {
                Logger.getLogger(RssFeedStreamParser.class.getName()).log(Level.SEVERE, null, ex);
            }
        return lastModifiedDate;
    }
    

    /**
     * Static method to get the date of the last modification to the url file
     * <p>
     * @see getRssFeedLastModifiedDate
     * 
     * @param urlFile a String containing a url
     * @return a string containing the last modified date of the url file
     */
    public static String getURLLastModifiedDate(String urlFile) 
    {
        String lastModifiedDate = "";
        try {
                lastModifiedDate = new URL(urlFile).openConnection().getHeaderField("Last-Modified");
            } catch (IOException ex) {
                Logger.getLogger(RssFeedStreamParser.class.getName()).log(Level.SEVERE, null, ex);
            }
        return lastModifiedDate;
    }
    

    
}
